﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/3/22 星期五 0:52:53
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Framework.EntityType;
using Sector7.Framework.DB;

namespace Sector7.Framework.DAO.Standard
{
    /// <summary>
    /// <!-- The description about this class. -->
    /// </summary>
    public class ValueTypeDAO : DAOBase<S7_ValueType>, IValueTypeDAO
    {
        #region Public functions
        public override object Create(S7_ValueType entity)
        {
            string sql = @"
                INSERT INTO S7_ValueType
                        (TypeName
                        ,TypeIdentifier
                        ,TypeDescription)
                    VALUES
                        (@TypeName
                        ,@TypeIdentifier
                        ,@TypeDescription)
                SELECT @@IDENTITY";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteScalar(sql,
                    DBUtil.CreateSqlParameter(entity.TypeName, "TypeName"),
                    DBUtil.CreateSqlParameter(entity.TypeIdentifier, "TypeIdentifier"),
                    DBUtil.CreateSqlParameter(entity.TypeDescription, "TypeDescription"));
            }
        }

        public override List<S7_ValueType> ReadAll()
        {
            List<S7_ValueType> types = new List<S7_ValueType>();

            string sql = @"
                SELECT *
                    FROM S7_ValueType";

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql);

                while (reader.Read())
                {
                    object type = new S7_ValueType();
                    DBUtil.ReadData(reader, ref type);

                    types.Add(type as S7_ValueType);
                }
            }

            return types;
        }
        #endregion
    }
}